import storage from '@gis/lib/utils/storage.js';
import { getLngLat } from './mapUtils.js';

export default {
  name: 'CenterZoomRecorder',
  props: {
    storeKey: { type: String, default: 'common' },
  },
  render() {
    return null;
  },
  created() {
    this.map = this.$parent.map;
    this.recoverCenterZoom();
  },
  unmounted() {
    this.saveCenterZoom();
  },
  methods: {
    getStoreKey() {
      return '_map_center_zoom' + (this.storeKey || '');
    },
    saveCenterZoom() {
      storage.local.setItem(this.getStoreKey(), {
        center: this.map.getCenter(),
        zoom: this.map.getZoom(),
      });
    },
    recoverCenterZoom() {
      const opt = storage.local.getItem(this.getStoreKey());
      if (opt) {
        this.map.setCenter(getLngLat(opt.center));
        this.map.setZoom(opt.zoom);
      }
    },
  },
};
